home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / serveraccept.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-14  |  3.8 KB  |  157 lines

  1. RCS_ID_C="$Id: serveraccept.c,v 1.2 1993/10/15 01:15:36 ppessi Exp $";
  2. /*
  3.  * Author: ppessi <Pekka.Pessi@hut.fi>
  4.  *
  5.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  6.  *                  Helsinki University of Technology, Finland.
  7.  *                  All rights reserved.
  8.  *
  9.  * Created      : Tue May 25 19:30:13 1993 ppessi
  10.  * Last modified: Fri Oct 15 00:50:51 1993 ppessi
  11.  * 
  12.  * $Log: serveraccept.c,v $
  13.  * Revision 1.2  1993/10/15  01:15:36  ppessi
  14.  * Use now StrToLong()
  15.  *
  16.  * Revision 1.1  1993/06/03  23:27:19  ppessi
  17.  * Cosmetic changes for version 1.0
  18.  *
  19.  */
  20.  
  21. /****** inetdlib.doc/serveraccept *******************************************
  22. *
  23. *  NAME
  24. *       serveraccept - Accept a server connection on named port
  25. *
  26. *  SYNOPSIS
  27. *       socket = serveraccept(name, peer);
  28. *
  29. *       long serveraccept(char *, struct sockaddr_in *);
  30. *
  31. *  DESCRIPTION
  32. *       The serveraccept() library call binds a socket to the named Internet
  33. *       TCP port. Then it listens the socket and accepts the connection to
  34. *       the port. The peer's socket address is returned in sockaddr pointed
  35. *       by sockaddr argument.
  36. *
  37. *       The port name is resolved by getservbyname() call. A numeric value
  38. *       for port name is also accepted.
  39. *       This module is meant for daemon developing.
  40. *
  41. *  INPUTS
  42. *       name     - port name or numeric string.
  43. *       peer     - pointer to struct sockaddr_in
  44. *
  45. *  RESULT
  46. *        socket - positive socket id for success or -1 for failure.
  47. *
  48. *        peer   - sockaddr_in structure containing peer's internet address.
  49. *                 Note that on error, the structure containing peer address
  50. *                 is not necessarily updated.
  51. *
  52. *  AUTHOR
  53. *       Pekka Pessi, the AmiTCP/IP Group, <amitcp-group@hut.fi>,
  54. *       Helsinki University of Technology, Finland.
  55. *
  56. *  SEE ALSO
  57. *        bsdsocket/accept, bsdsocket/getservbyname
  58. *
  59. *****************************************************************************
  60. *
  61. */
  62.  
  63. #ifdef AMIGA
  64. #if __SASC
  65. #include <proto/socket.h>
  66. #include <proto/dos.h>
  67. #include <clib/exec_protos.h>
  68. #include <pragmas/exec_sysbase_pragmas.h>
  69. #elif __GNUC__
  70. #include <inline/socket.h>
  71. #include <inline/exec.h>
  72. #else
  73. #include <clib/socket_protos.h>
  74. #endif
  75. #endif /* AMIGA */
  76.  
  77. #include <errno.h>
  78. #include <netdb.h>
  79.  
  80. #include <sys/param.h>
  81. #include <sys/socket.h>
  82. #include <sys/ioctl.h>
  83. #include <netinet/in.h>
  84.  
  85. #include <signal.h>
  86.  
  87. #include <dos/dos.h>
  88. #include <exec/execbase.h>
  89. #include <dos/var.h>
  90.  
  91. #include <stdlib.h>
  92.  
  93. /*
  94.  * serveraccept:
  95.  *      Accept a server socket from the named port
  96.  */
  97. long
  98. serveraccept(char *pname, struct sockaddr_in *ha)
  99. {
  100.   struct sockaddr_in sin; 
  101.   long ha_len = sizeof(*ha);
  102.   int s, sa;
  103.   long port;
  104.   struct servent *sp;
  105.   long on = 1;
  106.  
  107.   /* Create address corresponding our service */
  108.   bzero((caddr_t)&sin, sizeof(sin));
  109. #ifdef BSD4_4
  110.   sin.sin_len = sizeof(struct sockaddr_in);
  111. #endif
  112.   sin.sin_family = AF_INET;
  113.  
  114.   /* A port must be in the range 1 - 65535 */
  115.   if (StrToLong(pname, &port) > 0 && port < 65536 )
  116.     sin.sin_port = port;
  117.   else if (sp = getservbyname(pname, "tcp"))
  118.     sin.sin_port = sp->s_port;
  119.   else {
  120.     return -1;
  121.   }
  122.  
  123.   sin.sin_addr.s_addr = INADDR_ANY;
  124.  
  125.   if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  126.     PrintNetFault(Errno(), "socket");
  127.     return -1;
  128.   }
  129.  
  130.   /* Reuse this port */
  131.   if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
  132.     PrintNetFault(Errno(), "setsockopt");
  133.     sa = -1; goto Return;
  134.   }
  135.  
  136.   /* Bind it to socket */
  137.   if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0 ) {
  138.     PrintNetFault(Errno(), "bind");
  139.     sa = -1; goto Return;
  140.   }
  141.  
  142.   if (listen(s, 1) < 0) {
  143.     PrintNetFault(Errno(), "listen");
  144.     sa = -1; goto Return;
  145.   }
  146.  
  147.   if ((sa = accept(s, (struct sockaddr *)ha, &ha_len)) < 0){
  148.     PrintNetFault(Errno(), "accept");
  149.   }
  150.  
  151.  Return:
  152.   CloseSocket(s);
  153.   return sa;
  154. }
  155.  
  156.